﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Radiation___IM;
using System.Threading;
using Radiation.Common;

namespace RadiationUI
{
    public partial class Main : Form
    {
        private MdiClientController m_mdicontroller;

        private bool m_error_loading = false;
        private ReverseDnsWorker m_dns_worker;

        public bool ErrorLoading
        {
            get
            {
                return m_error_loading;
            }
        }

        public Main()
        {
            InitializeComponent();
            m_mdicontroller = new MdiClientController(this);
            m_mdicontroller.BorderStyle = BorderStyle.None;
            m_dns_worker = new ReverseDnsWorker();
            m_dns_worker.DomainFound += new EventHandler<GenericEventArgs>(m_dns_worker_DomainFound);
        }

        void m_dns_worker_DomainFound(object sender, GenericEventArgs e)
        {
            string domain = (string)e["message"];

            if (domain.Length > 50)
            {
                domain = domain.Substring(0, 50) + "...";
                serverToolStrip.ToolTipText = (string)e["message"];
            }

            serverToolStrip.Text += "(" + domain + ")";
        }

        private void quitterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ChannelNameDialog addchan = new ChannelNameDialog(this);
            addchan.ShowDialog();
        }

        private void Main_Load(object sender, EventArgs e)
        {
            WindowManager.Instance.addChannel("default", this);
            WindowManager.Instance["default"].Show();

            m_error_loading = Connection.initConnection();

            Connection.NicknameModified += new EventHandler<Radiation.Common.GenericEventArgs>(Connection_NicknameModified);
            Connection.FriendAdded += new EventHandler<GenericEventArgs>(Connection_FriendAdded);
            Connection.FriendRemoved += new EventHandler<GenericEventArgs>(Connection_FriendRemoved);
            Connection.FriendListCleared += new EventHandler<GenericEventArgs>(Connection_FriendListCleared);
            Connection.FriendListRefreshed += new EventHandler<GenericEventArgs>(Connection_FriendListRefreshed);

            versionToolstripLabel.Text = Version.VersionString;
            serverToolStrip.Text = Connection.IP + ":" + Convert.ToString(Connection.Port);
            m_dns_worker.SearchDomain(Connection.IP);
        }

        void Connection_FriendListRefreshed(object sender, GenericEventArgs e)
        {
            MethodInvoker action = delegate
            {
                string[] friends = (string[])e["message"];
                this.listBox1.Items.AddRange(friends);
            };
            this.BeginInvoke(action);
        }

        void Connection_FriendListCleared(object sender, GenericEventArgs e)
        {
            MethodInvoker action = delegate
            {
                listBox1.Items.Clear();
            };
            this.BeginInvoke(action);
        }

        void Connection_FriendRemoved(object sender, GenericEventArgs e)
        {
            MethodInvoker action = delegate
            {
                if (this.listBox1.Items.Contains((string)e["message"]))
                    this.listBox1.Items.Remove((string)e["message"]);
            };
            this.BeginInvoke(action);
        }

        void Connection_FriendAdded(object sender, GenericEventArgs e)
        {
            MethodInvoker action = delegate {
                if (!this.listBox1.Items.Contains((string)e["message"]))
                    this.listBox1.Items.Add((string)e["message"]);
            };
            this.BeginInvoke(action);
        }

        void Connection_NicknameModified(object sender, Radiation.Common.GenericEventArgs e)
        {
            this.toolStripStatusLabel2.Text = (string)e["message"];
        }

        private void Main_FormClosed(object sender, FormClosedEventArgs e)
        {
            Environment.Exit(0);
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            ChannelNameDialog addchan = new ChannelNameDialog(this);
            addchan.ShowDialog();
        }

        private void cascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.LayoutMdi(MdiLayout.Cascade);
        }

        private void horizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void verticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.LayoutMdi(MdiLayout.TileVertical);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Forms.UserLogin login = new Forms.UserLogin();
            login.ShowDialog();
        }

        private void logToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Forms.RealtimeLog.CanBeOpened)
            {
                Forms.RealtimeLog log = new Forms.RealtimeLog();
                log.Show();
            }
            else
                MessageBox.Show("Le log ne peut pas être ouvert 2 fois en même temps.", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
                Connection.sendToAPI(Radiation.Common.Communication.APIMethods.CLIENT_SET_INVISIBLE);
            else
                Connection.sendToAPI(Radiation.Common.Communication.APIMethods.CLIENT_SET_VISIBLE);
        }

        private void listBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex >= 0 && listBox1.SelectedIndex < listBox1.Items.Count)
            {
                button2.Enabled = true;
            }
            else
                button2.Enabled = false;
        }

        private void addFriendButton_Click(object sender, EventArgs e)
        {
            Forms.AddFriend add_friend = new Forms.AddFriend();
            add_friend.ShowDialog();
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            if (MessageBox.Show("Êtes vous sûr de vouloir enelver ce compte de vos amis ?", "Attention", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Yes)
            {
                Connection.sendToAPI(Radiation.Common.Communication.APIMethods.REMOVE_FRIEND + " " + (string)listBox1.SelectedItem);
            }
        }

        private void àProposToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Forms.About about = new Forms.About();
            about.Show();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            Forms.CreateAccount create_acc = new Forms.CreateAccount();
            create_acc.ShowDialog();
        }

        private void Main_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Control)
            {
                switch (e.KeyCode)
                {
                    case Keys.N: // New account
                        toolStripButton3_Click(null, null);
                        break;
                    case Keys.L: // Login
                        button2_Click(null, null);
                        break;
                    case Keys.J: // Join channel
                        button1_Click(null, null);
                        break;
                    case Keys.F2: // Log
                        logToolStripMenuItem_Click(null, null);
                        break;
                }
            }
        }

        private void administrationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Forms.Admin admin = new Forms.Admin();
            admin.Show();
        }
    }
}
